package com.kfm.base.function2;

import java.util.Arrays;

public class Exer01 {

    public static int sum(int ... nums){
        int sum = 0;
        for (int i = 0; i < nums.length; i++) {
            sum += nums[i];
        }
        return sum;
    }

    /**
     * 判断是否为素数（质数）
     * 奇数 不能被 2 整除的数 return num % 2 != 0;
     * @param num
     * @return
     */
    public static boolean isPrime(int num){
        if (num < 2){
            return false;
        }
        for (int i = 2; i < num; i++) {
            if (num % i == 0){
                return false;
            }
        }
        return true;
    }

    /**
     * 翻转
     * @param chs
     */
    public static char[] reverse(char[] chs){
        char[] newChar = new char[chs.length];
        int index = 0;
        for (int i = chs.length - 1; i >= 0; i--) {
            newChar[index ++] = chs[i];
        }
        return newChar;
    }

    /**
     * 否为回文数
     * @param num
     * @return
     */
    public static boolean isPalindromic(int num){
        int n = 0;
        int s = num;
        while(num != 0){
            int t = num % 10;
            n = n * 10 + t;
            num /= 10;
        }
        return n == s;
    }

    /**
     * 数组中的元素左移 k 个位置
     * @param arr
     * @param k
     * @return
     *
     *    1 2 3 4 5   3
     *    4 5 0 0 0
     */
    public static int[] left(int[] arr, int k){
        int[] nums = new int[arr.length];

        System.arraycopy(arr, k, nums, 0, arr.length - k);

        System.arraycopy(arr, 0, nums, arr.length - k,  k);

        return nums;
    }

    public static void reverse1(char[] chs){
        for (int i = 0; i < chs.length / 2; i ++) {
            char temp = chs[i];
            chs[i] = chs[chs.length - 1 - i];
            chs[chs.length - 1 - i] = temp;
        }
    }

    public static void main(String[] args) {
        int[] chs = {1, 2, 3, 4, 5};
        chs = left(chs, 6);

        System.out.println(Arrays.toString(chs));


    }
}
